package com.example.sunsg.okhttptest.Manager;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.example.sunsg.okhttptest.IBindPoll;
import com.example.sunsg.okhttptest.bean.IMyAidlInterfaceImpl;
import com.example.sunsg.okhttptest.bean.IMyInterfaceAddBookImpl;
import com.example.sunsg.okhttptest.service.BindPollServeice;

import java.util.concurrent.CountDownLatch;

/**
 * Created by sunsg on 15/12/8.
 */
public class BinderPollManager {
    public static final int IMYAIDLINTERFACE = 0;//normal
    public static final int IMYAIDLINTERFACE_ADD_BOOK = 1;//add Book
    private Context mContext;
    private CountDownLatch mConnectBinderPoolCountDownLatch;
    private IBindPoll mBindPool;
    public static BinderPollManager mInstance;
    public static BinderPollManager getInstance(Context context){
        if(mInstance == null){
            synchronized (BinderPollManager.class){
                if(mInstance == null){
                    mInstance = new BinderPollManager(context);
                }
            }
        }
        return mInstance;
    }

    public BinderPollManager(Context context){
        mContext = context.getApplicationContext();
        connectService();
    }

    private synchronized  void connectService(){
        mConnectBinderPoolCountDownLatch = new CountDownLatch(1);
        Intent intent = new Intent(mContext, BindPollServeice.class);
        mContext.bindService(intent,connection,Context.BIND_AUTO_CREATE);
        try {
            mConnectBinderPoolCountDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBindPool = IBindPoll.Stub.asInterface(service);
            try {
                mBindPool.asBinder().linkToDeath(mBinderPoolDeathRecipient,0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            mConnectBinderPoolCountDownLatch.countDown();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

   private IBinder.DeathRecipient mBinderPoolDeathRecipient = new IBinder.DeathRecipient(){
       @Override
       public void binderDied() {
           mBindPool.asBinder().unlinkToDeath(mBinderPoolDeathRecipient,0);
           mBindPool = null;
           connectService();
       }
   };

    public IBinder queryBinder(int bindCode){
        IBinder binder = null;
        if(mBindPool != null){
            try {
                binder = mBindPool.queryBinder(bindCode);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return binder;
    }


    public static class BindPollImpl extends IBindPoll.Stub{
        @Override
        public IBinder queryBinder(int bindCode) throws RemoteException {
            IBinder binder = null;
            switch (bindCode){
                case IMYAIDLINTERFACE:
                    binder = new IMyAidlInterfaceImpl();
                    break;
                case IMYAIDLINTERFACE_ADD_BOOK:
                    binder = new IMyInterfaceAddBookImpl();
                    break;
            }
            return binder;
        }
    }

}
